package com.example.aop;

import com.example.mapper.EmpLoginLogMapper;
import com.example.mapper.OperateLogMapper;
import com.example.pojo.Emp;
import com.example.pojo.EmpLoginLog;
import com.example.pojo.LoginInfo;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Auther: Sun
 * @Date: 2025/9/28 - 09 - 28 - 14:15
 * @Description: com.example.aop
 */
@Slf4j
@Aspect
@Component
public class OperateLoginLogAspect {
    @Autowired
    private EmpLoginLogMapper empLoginLogMapper;
    @Around("execution(* com.example.service.EmpService.login(..))")
    public Object saveLoginLog(ProceedingJoinPoint pjp) throws Throwable {
        Long start = System.currentTimeMillis();
        LoginInfo loginInfo =(LoginInfo) pjp.proceed();
        Long end = System.currentTimeMillis();


        Object[] args = pjp.getArgs();
        Emp loginEmp = (Emp) args[0];
        EmpLoginLog empLoginLog = new EmpLoginLog();
        empLoginLog.setUsername(loginEmp.getUsername());
        empLoginLog.setPassword(loginEmp.getPassword());
        empLoginLog.setLoginTime(LocalDateTime.now());
        empLoginLog.setCostTime(end-start);
        empLoginLog.setIsSuccess(loginInfo == null ? (short)0 : (short)1);
        empLoginLog.setJwt(loginInfo == null ? null : loginInfo.getToken());
        empLoginLogMapper.insert(empLoginLog);

    return loginInfo;

    }
}
